package androidx.blogger;

import java.util.List;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.blogger.activities.ShowUserBlogs;

public class LoginActivity extends Activity {
  private static final int SERVICE_LOGIN_ID = 1;
  private static final String IDENTITIES_APPLICATION_URI =
      "market://search?q=pname:androidx.identities";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = new Intent("androidx.intent.action");
    intent.putExtra("SERVICE", "glass");
    List<ResolveInfo> handlers =
        getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    if (handlers.isEmpty()) {
      Log.i("Android-identities is not installed!");
      setInstallContentView();
    } else {
      setLoginContentView();
    }
  }

  private void setInstallContentView() {
    setContentView(R.layout.install_identities);
    Button btn = (Button) findViewById(R.id.install_btn);
    btn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        installAndroidIdentities();
      }
    });
  }

  private void installAndroidIdentities() {
    shortMessage("Please install Android-Identities");
    Intent intent = new Intent();
    intent.setData(Uri.parse(IDENTITIES_APPLICATION_URI));
    try {
      startActivity(intent);
    } catch (ActivityNotFoundException anfe2) {
      longMessage("Android Market not found!");
    }
    finish();
  }


  private void setLoginContentView() {
    String auth = BloggerApplication.getInstance().getAuth();
    if (auth != null) {
      Log.i("Already logged into blogger");
      launchMainActivity();
      return;
    }

    setContentView(R.layout.login);
    Button btn = (Button) findViewById(R.id.login_btn);
    btn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        doLogin();
      }
    });
  }


  private void doLogin() {
    Intent intent = new Intent("androidx.intent.action");
    intent.putExtra("SERVICE", "blogger");
    startActivityForResult(intent, SERVICE_LOGIN_ID);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent result) {
    if (requestCode == SERVICE_LOGIN_ID) {
      onLoginResult(resultCode, result);
    }
    super.onActivityResult(resultCode, resultCode, result);
  }


  private void onLoginResult(int resultCode, Intent result) {
    if (resultCode == RESULT_OK) {
      String auth = result.getStringExtra("AUTH");
      String identity = result.getStringExtra("IDENTITY");
      Log.i("Identity=" + identity + " auth=" + auth);
      String service = result.getStringExtra("SERVICE");
      Log.i("service=" + service);
      if ("blogger".equals(service)) {
        BloggerApplication.getInstance().setAuth(identity, auth);
        launchMainActivity();
      }
    } else {
      shortMessage("Login unsuccessful");
      Log.i("Login unsuccessful");
    }
  }

  private void launchMainActivity() {
    Log.i("launchMainActivity");
    // Intent intent = new Intent(this, MainActivity.class);
    Intent intent = new Intent(this, ShowUserBlogs.class);
    startActivity(intent);
    finish();
  }


  private void shortMessage(String msg) {
    Toast toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
    toast.show();
  }

  private void longMessage(String msg) {
    Toast toast = Toast.makeText(this, msg, Toast.LENGTH_LONG);
    toast.show();
  }

}
